package com.xzc.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xzc.auth.mapper.SysRoleMapper;
import com.xzc.auth.service.SysRoleService;
import com.xzc.auth.service.SysUserRoleService;
import com.xzc.auth.service.SysUserService;
import com.xzc.model.system.SysRole;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xzc.model.system.SysUserRole;
import com.xzc.vo.system.AssginRoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 徐生
 * @version 1.0
 * Date 2024/3/23 20:15
 * Description: SysRoleServiceImpl
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
    @Autowired
    private SysUserRoleService sysUserRoleService;

    //查询所有角色 和 当前用户所属角色
    public Map<String, Object> findRoleByAdminId(Long userId) {
        //1、查询所有角色，返回list集合，返回
        List<SysRole> allRoleList = baseMapper.selectList(null);

        //2、根据userId查询角色用户关系表，查询userId对应所有角色id
        LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper<>();//编写条件
        wrapper.eq(SysUserRole::getUserId,userId);//根据userId进行查询
        List<SysUserRole> existUserRoleList = sysUserRoleService.list(wrapper);

        //从查询出来的用户id对应角色list集合，获取所有角色id
        List<Long> existUserRoleIdList = existUserRoleList.stream().map(c -> c.getRoleId()).collect(Collectors.toList());
        //传统方式遍历：
        /*List<Long> list = new ArrayList<>();
        for (SysUserRole sysUserRole : existUserRoleList) {
            Long roleId = sysUserRole.getRoleId();
            list.add(roleId);
        }*/

        //3、根据查询所有角色id，找到对应角色信息
        //根据角色id到所有的角色的list集合进行比较
        List<SysRole> assignRoleList = new ArrayList<>();
        for (SysRole sysRole : assignRoleList) {
            //比较
            if (existUserRoleList.contains(sysRole.getId())) {
                assignRoleList.add(sysRole);
            }
        }

        //4、把得到两部分数据封装map集合，返回
        Map<String, Object> roleMap = new HashMap<>();
        roleMap.put("assginRoleList", assignRoleList);
        roleMap.put("allRolesList", allRoleList);
        return roleMap;
    }

    //根据用户分配角色
    public void doAssign(AssginRoleVo assginRoleVo) {
        //把用户之前分配角色数据删除，用户角色关系表里面，更具userId删除
        LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper();
        wrapper.eq(SysUserRole::getUserId,assginRoleVo.getUserId());
        //重写进行分配
        List<Long> roleIdList = assginRoleVo.getRoleIdList();
        for (Long roleId : roleIdList) {
            //不为空则添加
            if(!StringUtils.isEmpty(roleId)){
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setUserId(assginRoleVo.getUserId());
                sysUserRole.setRoleId(roleId);
                sysUserRoleService.save(sysUserRole);
            }
        }
    }
}
